<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <title>拍卖商品管理页</title>
  <link href="css/common.css" rel="stylesheet" type="text/css" />
  <link href="css/style.css" rel="stylesheet" type="text/css" />
  <style>
    .page {
      margin: 20px 0;
      text-align: center;
    }
    .page a, .page span {
      display: inline-block;
      padding: 5px 10px;
      margin: 0 3px;
      border: 1px solid #ddd;
      text-decoration: none;
      color: #333;
    }
    .page a:hover {
      background: #f5f5f5;
    }
    .page .current {
      background: #e4393c;
      color: white;
      border-color: #e4393c;
    }
    .page .disabled {
      color: #ccc;
      pointer-events: none;
    }
  </style>
</head>
<body>
<div class="wrap">
  <!-- main begin-->
  <div class="sale">
    <h1 class="lf">在线拍卖系统</h1>
    <div class="logout right"><a href="${pageContext.request.contextPath}/logout" title="注销">注销</a></div>
  </div>

  <!-- 查询表单 -->
  <div class="forms">
    <form action="${pageContext.request.contextPath}/do/pageList" method="get">
      <input type="hidden" name="pageNum" id="pageNum" value="1"/>
      <input type="hidden" name="pageSize" id="pageSize" value="5"/>

      <label for="name">名称</label>
      <input name="auctionName" type="text" class="nwinput" id="name"/>

      <label for="names">描述</label>
      <input name="auctionDesc" type="text" id="names" class="nwinput"/>

      <label for="time">开始时间</label>
      <input name="auctionStartTime" type="text" id="time" class="nwinput" />

      <label for="end-time">结束时间</label>
      <input name="auctionEndTime" type="text" id="end-time" class="nwinput"/>

      <label for="price">起拍价</label>
      <input name="auctionStartPrice" type="text" id="price" class="nwinput"  min="0" step="0.01"/>

      <input type="submit" value="查询" class="spbg buttombg f14 sale-buttom" onclick="search(1)"/>
      <input type="button" value="发布" onclick="location.href='addAuction.jsp'" class="spbg buttombg f14 sale-buttom buttomb"/>
    </form>
  </div>

  <!-- 数据显示 -->
  <div class="items">
    <c:if test="${not empty error}">
      <div class="error">${error}</div>
    </c:if>

    <c:if test="${not empty pageInfo}">
      <!-- 表格标题 -->
      <ul class="rows even strong">
        <li>名称</li>
        <li>描述</li>
        <li>开始时间</li>
        <li>结束时间</li>
        <li>起拍价</li>
        <li>操作</li>
      </ul>

      <!-- 数据行 -->
      <c:forEach items="${pageInfo.list}" var="auction" varStatus="status">
        <ul class="rows ${status.index % 2 == 0 ? 'even' : ''}">
          <li>${auction.auctionName}</li>
          <li>${auction.auctionDesc}</li>
          <li><fmt:formatDate value="${auction.auctionStartTime}" pattern="yyyy-MM-dd"/></li>
          <li><fmt:formatDate value="${auction.auctionEndTime}" pattern="yyyy-MM-dd"/></li>
          <li><fmt:formatNumber value="${auction.auctionStartPrice}" type="currency"/></li>
          <li class="borderno red">
            <a href="${pageContext.request.contextPath}/do/selectById?auctionId=${auction.auctionId}">修改</a> |
            <a href="#" onclick="return confirmDelete(${auction.auctionId})">删除</a>
          </li>
        </ul>
      </c:forEach>

      <!-- 分页 -->
      <div class="page">
        <c:if test="${pageInfo.hasPreviousPage}">
          <a href="javascript:goPage(${pageInfo.prePage})">上一页</a>
        </c:if>

        <c:forEach begin="1" end="${pageInfo.pages}" var="i">
          <c:choose>
            <c:when test="${i == pageInfo.pageNum}">
              <span class="current">${i}</span>
            </c:when>
            <c:otherwise>
              <a href="javascript:goPage(${i})">${i}</a>
            </c:otherwise>
          </c:choose>
        </c:forEach>

        <c:if test="${pageInfo.hasNextPage}">
          <a href="javascript:goPage(${pageInfo.nextPage})">下一页</a>
        </c:if>
      </div>
    </c:if>
  </div>
</div>
<script>
  function goPage(pageNum) {
    const form = document.createElement('form');
    form.method = 'get';
    form.action = '${pageContext.request.contextPath}/do/pageList';

    // 添加分页参数
    addHiddenInput(form, 'pageNum', pageNum);
    addHiddenInput(form, 'pageSize', ${pageInfo.pageSize});

    // 添加原有查询参数
    <c:forEach items="${queryParams}" var="param">
    <c:if test="${param.key != 'pageNum' && param.key != 'pageSize'}">
    addHiddenInput(form, '${param.key}', '${param.value[0]}');
    </c:if>
    </c:forEach>

    document.body.appendChild(form);
    form.submit();
  }

  function confirmDelete(auctionId) {
    if (confirm('确定要删除这个拍卖项吗？')) {
      location.href ='${pageContext.request.contextPath}/do/deleteAuction?auctionId=' + auctionId;
    }
    return false;
  }

  function addHiddenInput(form, name, value) {
    const input = document.createElement('input');
    input.type = 'hidden';
    input.name = name;
    input.value = value;
    form.appendChild(input);
  }
</script>
</body>
</html>